package com.bw.car.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.bw.car.domain.Car;
import com.bw.car.domain.CarType;
import com.bw.car.domain.OutIn;
import com.bw.car.service.CarService;
import com.bw.car.vo.CarVO;
import com.github.pagehelper.PageInfo;
//汽车
@Controller
public class CarController {
	@Autowired
	CarService carService;
	
	
	@PostMapping("outStore")
	public String outStore(OutIn outIn) {
		carService.outStore(outIn);
		
		return "redirect:list";
		
	}
	
	@GetMapping("outStore")
	public String outStore(Model model,Integer id) {
		
		model.addAttribute("money",carService.getMoneyByCid(id));
		model.addAttribute("id",id);
		return "out_store";
	}
	@ResponseBody
	@GetMapping("getCarInfo")
	public Car getCarInfo(String cno) {
		return carService.getCarInfo(cno);
	}
	
	
	

	// 调转到增加页面
	@GetMapping("add")
	public String add(Model model) {
		// 获取所有汽车类型
		model.addAttribute("types", CarType.values());
		model.addAttribute("inTime", new Date());// 获取系统时间
		return "add";

	}

	// 执行增加
	@PostMapping("add")
	public String add(Car car, String inTime) {

		carService.insert(car, inTime);

		return "redirect:list";

	}

	// 列表页面
	@RequestMapping("list")
	public String list(Model model, Car car, String [] colors,@RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "3") Integer pageSize) {
		PageInfo<Car> info = carService.selects(car, pageNum, pageSize);
		model.addAttribute("info", info);
		// 获取所有汽车类型
		model.addAttribute("types", CarType.values());
		model.addAttribute("names", JSON.toJSONString(colors));

		return "list";

	}
	@RequestMapping("getReport")
	public String getReport(Model model,CarVO carVO) {
		
		List<OutIn> report = carService.getReport(carVO);
		model.addAttribute("report", report);
		model.addAttribute("carVO", carVO);
		return "report";
	}

}
